package com.itwxz.forum.controller.mobile;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itwxz.forum.domain.dto.CommentDto;
import com.itwxz.forum.domain.po.ForumCommentChild;
import com.itwxz.forum.domain.po.ForumCommentParent;
import com.itwxz.forum.domain.vo.CommentParentVo;
import com.itwxz.forum.service.ForumCommentChildService;
import com.itwxz.forum.service.ForumCommentParentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Tag(name = "评论接口", description = "移动端帖子评论相关接口")
@RequestMapping("/forum/mobile/comment")
public class ForumCommentMobileController {
    @Autowired
    private ForumCommentParentService commentParentService;
    @Autowired
    private ForumCommentChildService commentChildService;

    @Operation(
            summary = "添加一级评论",
            description = "为指定帖子添加一级评论（父评论）"
    )
    @PostMapping("/parent")
    public SaResult addParentComment(@RequestBody CommentDto dto) {
        return commentParentService.addParentComment(dto);
    }

    @Operation(
            summary = "添加二级评论",
            description = "为某条一级评论添加二级评论（子评论/回复）"
    )
    @PostMapping("/child")
    public SaResult addChildComment(@RequestBody CommentDto dto) {
        return commentChildService.addChildComment(dto);
    }

    @Operation(
            summary = "获取一级评论列表",
            description = "分页获取某个帖子下的所有一级评论及其子评论"
    )
    @GetMapping("/parent/list")
    public SaResult getParentComments(@RequestParam Long postId,
                                      @RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "10") int pageSize) {
        IPage<CommentParentVo> page = commentParentService.getParentCommentsWithChild(postId, pageNum, pageSize);
        return SaResult.data(page);
    }

    @DeleteMapping("/parent/{commentId}")
    @Operation(summary = "删除一级评论", description = "根据评论 ID 删除当前用户的一级评论")
    public SaResult delParentComment(@PathVariable String commentId) {
        return commentParentService.deleteParentComment(commentId);
    }

    @DeleteMapping("/child/{commentId}")
    public SaResult delChildComment(@PathVariable String commentId) {
        return commentChildService.deleteChildComment(commentId);
    }
}
